package com.ctgu.fastleader.controller;

import com.ctgu.fastleader.service.ShelfService;
import com.ctgu.fastleader.vo.Shelf;
import com.ctgu.fastleader.vo.ShelfKey;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


@Controller
@RequestMapping("/shelf")
public class ShelfController {

    @Resource
    ShelfService shelfService;

    @RequestMapping("toAddShelf")
    public String toAddShelf(Integer houseid, String housename, Model model){
        model.addAttribute("houseid",houseid);
        model.addAttribute("housename",housename);
        return "forward:/page/toShelfAdd";
    }

    @ResponseBody
    @RequestMapping("/judgeShelf")
    public String judgeShelf(Integer houseid, String pianqu, String huojia, String huojiahao){
        if(huojiahao.length() == 1){
            huojiahao = "00" + huojiahao;
        }else if(huojiahao.length() == 2){
            huojiahao = "0" + huojiahao;
        }
        String shelfId = pianqu+"-"+huojia+"-"+huojiahao;
        ShelfKey shelfKey = new ShelfKey(shelfId,houseid);
        Shelf shelf = shelfService.selectByPrimaryKey(shelfKey);
        System.out.println("houseid:"+houseid);
        System.out.println("shelfid:"+shelfId);
        if(shelf == null){
            return "success";
        }else{
            return "fail";
        }
    }


    @ResponseBody
    @RequestMapping("/addShelf")
    public String addShelf(Integer houseid, String pianqu, String huojia, String huojiahao){
        if(huojiahao.length() == 1){
            huojiahao = "00" + huojiahao;
        }else if(huojiahao.length() == 2){
            huojiahao = "0" + huojiahao;
        }
        String shelfId = pianqu+"-"+huojia+"-"+huojiahao;
        System.out.println(houseid+"..."+shelfId);
        Shelf shelf = new Shelf(shelfId,houseid,0);
        shelfService.insert(shelf);
        return "success";
    }
}
